import type { ConfigEnv, UserConfig } from 'vite';
import { loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import path, { resolve } from 'path';
// import legacy from '@vitejs/plugin-legacy';
import UnoCss from 'unocss/vite';
import { visualizer } from 'rollup-plugin-visualizer';
// const mock = require('./build/mock/createMockServer');

export default ({ mode }: ConfigEnv): UserConfig => {
    const root = process.cwd();
    const env = loadEnv(mode, root);
    return {
        base: env.VITE_APP_PUBLIC_PATH,
        // 兼容 Cli
        define: {
            'process.env.VUE_APP_API_BASE_URL': JSON.stringify(env.VITE_APP_API_BASE_URL),
            'process.env.VUE_APP_PUBLIC_PATH': JSON.stringify(env.VITE_APP_PUBLIC_PATH),
        },
        plugins: [
            // legacy({
            //     targets: ['defaults', 'not IE 11', 'Chrome 63', 'Firefox > 20'],
            //     modernPolyfills: true,
            // }),
            vue(),
            vueJsx(),
            UnoCss(),
            // mock({
            //     watch: true,
            //     mockUrlList: [/api/],
            //     cwd: process.cwd(),
            //     enable: env.VITE_HTTP_MOCK === 'true' && process.env.NODE_ENV !== 'production',
            // }),
            visualizer(),
        ],
        build: {
            cssCodeSplit: false,
            chunkSizeWarningLimit: 2048,
            rollupOptions: {
                input: {
                    main: resolve(__dirname, 'index.html'),
                    subpage: resolve(__dirname, 'pages/index.html'),
                },
                output: {
                    manualChunks: {
                        vue: ['vue', 'pinia', 'vue-router'],
                        antdv: ['ant-design-vue', '@ant-design/icons-vue'],
                    },
                },
            },
        },
        resolve: {
            alias: {
                '~@': path.join(__dirname, './src'),
                '@': path.join(__dirname, './src'),
                '~': path.join(__dirname, './src/assets'),
                vue: 'vue/dist/vue.esm-bundler.js',
                dayjs: resolve(__dirname, 'node_modules', 'dayjs'),
                'ant-design-vue': resolve(__dirname, 'node_modules', 'ant-design-vue'),
            },
        },
        optimizeDeps: {
            include: [
                'ant-design-vue/es/locale/en_US',
                'ant-design-vue/es/locale/zh_CN',
                'store/plugins/expire',
                'ant-design-vue/es/form',
                'dayjs',
                'dayjs/locale/en',
                'dayjs/locale/zh-cn',
                '@ant-design/icons-vue',
                'lodash-es',
                'pinia',
                'vue-router',
                'vue',
                'vue-i18n',
                '@vueuse/core',
            ],
        },
        css: {
            preprocessorOptions: {
                less: {
                    modifyVars: {
                        hack: 'true; @import "~/styles/variables.less";',
                    },
                    // DO NOT REMOVE THIS LINE
                    javascriptEnabled: true,
                },
            },
        },
        server: {
            host: true,
            proxy: {
                '/api/oauth2': {
                    // backend url
                    target: env.VITE_API_BASE_URLL,
                    ws: false,
                    changeOrigin: true,
                    rewrite: path => path.replace(/^\/api/, ''),
                },
                '/api': {
                    // backend url
                    //   target: 'http://192.168.13.135:6789',
                    target: env.VITE_API_BASE_URL,
                    ws: false,
                    changeOrigin: true,
                    rewrite: path => path.replace(/^\/api/, ''),
                },
            },
        },
    };
};
